package jp.co.dailyReport.customer.sql;

import java.util.Calendar;
import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.customer.Customer;

/**
 * 取引先情報を更新するクラスです。
 */
public class CustomerUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(CustomerUpdateExecuter.class);
	private Customer customer;

	public CustomerUpdateExecuter(Customer customer) {
		this.customer = customer;
	}

	/**
	 * 取引先IDを基に、取引先情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update customer set name=?,upDay=? where customerId=?";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, Utilities.convUniqueCharacter(customer.getName()));
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(2, new Date(cal.getTime().getTime()));
			pstmt.setInt(3, customer.getCustomerId());
			pstmt.execute();
		} finally {
            if(pstmt != null)
                pstmt.close();
		}
		return null;
	}
}
